package com.supermap.dataservice.project.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.project.mapper.ExpertProductMapper;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.product.ExpertProductService;
import com.supermap.dubbo.api.product.entity.ExpertProduct;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @Author:zhoujian
 * @Desciption:
 * @date:2019-07-03 11:38
 * @version:v1.0.0
 */
@Service
public class ExpertProductServiceImpl implements ExpertProductService {

    @Autowired
    private ExpertProductMapper expertProductMapper;

    @Override
    public JSONUtils insertExpertProduct(JSONObject jsonObject) {

        //将json转换成实体
        ExpertProduct expertProduct = JSONObject.toJavaObject(jsonObject , ExpertProduct.class);
        //入库
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        int count = expertProductMapper.insertExpertProduct(expertProduct);
        if (count > 0)
        {
            return  new JSONUtils().setData(expertProduct).setCountSum(count);
        }else
        {
            return  new JSONUtils().setCountSum(count);
        }
    }

    @Override
    public JSONUtils queryByCropAndTypeList(Integer productType, String cropType, String cropsId, Integer cropsPubertyId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String , Object>> expertProductList = expertProductMapper.queryByCropAndTypeList(productType , cropType , cropsId , cropsPubertyId);
        return new JSONUtils().setData(expertProductList).setCountSum(expertProductList != null ? expertProductList.size():0);
    }


}